package cn.wolfcode.util;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: 用户信息工具类
 * @Author: 伍家钰
 * @Date: 2020/7/14 10:33
 * @Version: 1.0
 */
public class UserContext {
    /* *//**
     * 存储用户登入信息
     *//*
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";

    *//**
     * 存取用户权限表达式信息
     *//*
    public static final String USER_EXPRESSION_SESSION = "USER_EXPRESSION_SESSION";

    public static HttpSession getSession() {
        // Spring提供的工具,可以在任意地方难道请求对象(request)和响应对象(response)和session对象
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    *//**
     * 获取当前登入用户的权限表达式
     *
     * @return :
     *//*
    public static List<String> getExperssions() {
        return (List<String>) getSession().getAttribute(USER_EXPRESSION_SESSION);
    }

    *//**
     * 设置当前用户到session中
     *
     * @param list
     *//*
    public static void setExperssions(List<String> list) {
        getSession().setAttribute(USER_EXPRESSION_SESSION, list);
    }

    *//**
     * 获取当前登入用户
     *
     * @return :
     *//*
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    *//**
     * 设置当前用户到session中
     *
     * @param employee
     *//*
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }
*/

    /**
     * 从shiro中获取当前登入用户
     */
    public static Employee getEmployee() {
        return (Employee) SecurityUtils.getSubject().getPrincipal();
    }
}
